package com.libl.service.impl;

import com.libl.domain.Account;
import com.libl.mapper.AccountMapper;
import com.libl.service.AccountService;

public class AccountServiceImpl implements AccountService {
    //拿到dao层对象，也就是操作数据库的代理对象
    private AccountMapper mapper;
    //提供set方法赋值
    public void setMapper(AccountMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void transfer(String fromName, String toName, double money) {
        //编程式事务直接写到业务代码里，先开启事务
        //1，根据账户名查找账户
        Account fromAccount = mapper.findByName(fromName);
        Account toAccount = mapper.findByName(toName);
        //2，更改余额
        fromAccount.setMoney(fromAccount.getMoney()-money);
        toAccount.setMoney(toAccount.getMoney()+money);
        //3，更新到数据库
        mapper.update(fromAccount);

        System.out.println(1/0);//用异常测试事务回滚

        mapper.update(toAccount);

        //如果 catch异常  回滚事务
        //再提交事务
    }
}
